﻿<?php

include("inc_configuration.php");

$tid=0;
$t=0;
$y=0;
$yr=0;
$r=0;
$anz=0;

$q="SELECT type FROM $tbl_leagues WHERE id=$liga";
$qe=mysql_query($q);
$qeo=mysql_fetch_object($qe);
	
$type=$qeo->type;

if ($type==1)
{
	$tQ = "	
				SELECT

				t.id AS tid,
				t.name AS tname

				FROM 

				$tbl_teams t, 
				$tbl_lt lt,
				$tbl_events e,
				$tbl_matches m

				WHERE 

				lt.leagueId=$liga AND 
				lt.teamId=t.id AND
				m.id=e.matchId AND
				m.leagueId=$liga AND
				m.free=3
				
				GROUP BY t.id
				";
}
else
{
	$tQ = "	
				SELECT

				t.id AS tid,
				t.name AS tname

				FROM 

				$tbl_teams t, 
				$tbl_gt gt,
				$tbl_lg lg,
				$tbl_events e,
				$tbl_matches m

				WHERE 

				lg.leagueId=$liga AND 
				gt.teamId=t.id AND 
				gt.groupId=lg.id AND
				m.id=e.matchId AND
				m.leagueId=$liga AND
				m.stageId=lg.id AND
				m.free=3
				
				GROUP BY t.id
				";
}

//echo $tQ;

$tQe = mysql_query($tQ);

$platz=1;

while ($row = mysql_fetch_object($tQe))
{
	$tid=$row->tid;
	$tname=$row->tname;
	
	$n=$tname;
	
	$team = array(
			  teamId => $tid,
			  name => $n,
			  t => 0,
			  et => 0,
			  p =>0,
			  y => 0,
			  yr => 0,
			  r => 0);
	
	$teams[] = $team;
	$anz++;
	
}

if ($type==1)
{
	$mQ = "	
			SELECT

			t.id AS tid,
			t.name AS tname,
			e.type,
			COUNT(*) AS anz

			FROM 

			$tbl_teams t, 
			$tbl_lt lt,
			$tbl_events e,
			$tbl_matches m

			WHERE 

			lt.leagueId=$liga AND 
			lt.teamId=t.id AND
			t.id=e.teamId AND
			m.id=e.matchId AND
			m.leagueId=$liga AND
			m.free=3
			
			GROUP BY t.id,e.type
			";
}
else
{
	$mQ = "	
				SELECT

				t.id AS tid,
				t.name AS tname,
				e.type,
				COUNT(*) AS anz

				FROM 

				$tbl_teams t, 
				$tbl_gt gt,
				$tbl_lg lg,
				$tbl_events e,
				$tbl_matches m

				WHERE 

				lg.leagueId=$liga AND 
				gt.teamId=t.id AND 
				gt.groupId=lg.id AND
				m.id=e.matchId AND
				m.leagueId=$liga AND
				m.stageId=lg.id AND
				m.free=3
				
				GROUP BY e.teamId,e.type
				";
}

// echo $mQ;

			
$mQe = mysql_query($mQ);

while ($row = mysql_fetch_object($mQe))
{
	
	$tid=$row->tid;
	$type=$row->type;
	$tanz=$row->anz;	
	
	$count=0;
	$found=0;
	
	while (($count<$anz) AND ($found==0))
	{	
		$team=$teams[$count];
		
		$tI=$team["teamId"];
		
		if ($tI==$tid)
		{
			
			
			if ($type==1)
			{
				$team[t] = $team[t]+$tanz;
				$found=1;
			}
			else
				{
					if ($type==2)
					{
						$team[y] = $team[y]+$tanz;
						$found=1;
					}
					else
						{
							if ($type==3)
							{
								$team[yr] = $team[yr]+$tanz;
								$found=1;
							}
							else
								{
									if ($type==4)
									{
										$team[r] = $team[r]+$tanz;
										$found=1;
									}
									else
									{
										if ($type==5)
										{
											$team[t] = $team[t]+$tanz;
											$team[p] = $team[p]+$tanz;
											$found=1;
										}
										else
										{
											if ($type==6)
											{
												$team[et] = $team[et]+$tanz;
												$found=1;
											}
										}
									}
								}
						}
				}
			$teams[$count]=$team;
		}	
		
		$count++;
	
	
	}
}

	
$count=0;
for($count = 0; $count < $anz; $count++)
{
	$pA=$teams[$count];
	$userId=$jUser;
	$pn=$pA[name];
	$tid=$pA[teamId];
	$t=$pA[r]*2+$pA[y]+$pA[yr];
	$y=$pA[y];
	$yr=$pA[yr];
	$r=$pA[r];
	
	$tableQ="INSERT INTO 	$tbl_statistics (userId,name,teamid,t,p,y,yr,r,et) 
							VALUES ($userId,'$pn',$tid,$t,0,$y,$yr,$r,0)";
							
	$tableQe=mysql_query($tableQ);
	
	
}


$tsQ="	SELECT userId,name,teamid,t,p,y,yr,r,et 
		FROM $tbl_statistics 
		WHERE
			userId=$jUser
		ORDER BY t ASC, r ASC, yr ASC, y ASC";
$tsQe=mysql_query($tsQ);

echo "<center><table id='tabelle' width='400px'>";

echo "	<tr>		
					<th>Platz</th>
					<th colspan='2'>Team</th>
					<th>gelb</th>
					<th>gelb-rot</th>
					<th>rot</th>
				</tr>";
				



$gest=0;

	while ($row = mysql_fetch_object($tsQe))
	{
		$p=0;
		$pN=$row->name;
		$tid=$row->teamid;
		$t=$row->t;
		$y=$row->y;
		$yr=$row->yr;
		$r=$row->r;
		
		$gest=$gest+$t;
		
		echo "	<tr>
					<td>$platz.</td>
					<td><a href='spiele_team.php?team=$tid&liga=$liga'>$pN</a></td>
					<td><a href='team_kader.php?team=$tid'><img src='img/logos/",$tid,".png' width='35' height='35' alt='' align='middle'></a></td>
					<td>$y</td>
					<td>$yr</td>
					<td>$r</td>
				</tr>";
		$platz++;
		
	}
	
	echo "<tr id='tabellenkopf' heigth='5px'>
			<th colspan='8'></th></tr></table></center>";
	
	$l = "DELETE FROM $tbl_statistics WHERE userId=$jUser";
	$le = mysql_query($l);


?>